package com.example.airhockeytextured.objects

import android.opengl.GLES20
import com.example.airhockeytextured.Constants
import com.example.airhockeytextured.data.VertexArray
import com.example.airhockeytextured.program.ColorShaderProgram
import com.example.airhockeytextured.program.TextureShaderProgram

/**
 * @author Liujianyou
 * @date 2020/8/7
 * @des
 */
class Mallet {
    /**
     * 位置分量计数
     * */
    private val POSITION_COMPOMENT_COUNT = 2

    /**
     * 颜色分量计数
     * */
    private val COLOR_COMPOMENT_COUNT = 3

    private val vertexArray: VertexArray

    /**
     * 跨距
     * */
    private val STRIDE =
        (POSITION_COMPOMENT_COUNT + COLOR_COMPOMENT_COUNT) * Constants.BYTES_PER_FLOAT

    /**
     * 这个数组包含了空气曲棍球桌子的顶点数据。我们定义了x和y的位置，以及s和t纹理坐标
     * t分量需要按照y分量相反的方向定义
     * */
    private val VERTEX_DATA = floatArrayOf(
        //Order of coordinates:X,Y,R,G,B
        0f, -0.4f, 0f, 0f, 1f,
        0f, 0.4f, 1f, 0f, 0f

    )

    init {
        vertexArray = VertexArray(vertexData = VERTEX_DATA)
    }


    fun bindData(program: ColorShaderProgram) {
        vertexArray.setVertexAttributePointer(
            0,
            program.getPositionAttributeLocation(),
            POSITION_COMPOMENT_COUNT,
            STRIDE
        )
        vertexArray.setVertexAttributePointer(
            POSITION_COMPOMENT_COUNT,
            program.getColorAttributeLocation(),
            COLOR_COMPOMENT_COUNT,
            STRIDE
        )


    }

    fun draw() {
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 2)
    }
}